package com.elf.zero.app;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.elf.zero.app.databinding.ZeroDialogDefaultBinding;


/**
 * 通用默认对话框
 */
public class DefaultDialog extends AlertDialog {

    private String title, message, cancelText, okText;
    private View customContentView;
    private ViewGroup.LayoutParams layoutParams;
    private View.OnClickListener cancelListener, okListener;

    public DefaultDialog(@NonNull Context context) {
        super(context, R.style.ZeroDialogDefault);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.zero_dialog_default);

        ZeroDialogDefaultBinding binding = ZeroDialogDefaultBinding.inflate(getLayoutInflater(), new FrameLayout(getContext()), false);
        setContentView(binding.getRoot());

        if (!TextUtils.isEmpty(title)) {
            binding.tvTitle.setText(title);
            binding.tvTitle.setVisibility(View.VISIBLE);
            binding.viewContentTopSpace.setVisibility(View.GONE);
        } else {
            binding.tvTitle.setVisibility(View.GONE);
            binding.viewContentTopSpace.setVisibility(View.VISIBLE);
        }

        if (customContentView == null) {
            binding.tvMsg.setText(message);
        } else {
            binding.layoutContent.removeAllViews();
            if (layoutParams == null) {
                binding.layoutContent.addView(customContentView);
            } else {
                binding.layoutContent.addView(customContentView, layoutParams);
            }
        }

        if (!TextUtils.isEmpty(okText)) {
            binding.btnOk.setText(okText);
            binding.btnOk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (okListener != null) {
                        okListener.onClick(v);
                    } else {
                        dismiss();
                    }
                }
            });
            binding.btnOk.setVisibility(View.VISIBLE);
        } else {
            binding.btnOk.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(cancelText)) {
            binding.btnCancel.setText(cancelText);
            binding.btnCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (cancelListener != null) {
                        cancelListener.onClick(v);
                    } else {
                        dismiss();
                    }
                }
            });
            binding.btnCancel.setVisibility(View.VISIBLE);
        } else {
            binding.btnCancel.setVisibility(View.GONE);
        }

        View view_divider_h = findViewById(R.id.view_divider_h);
        if (view_divider_h != null) {
            if (binding.btnCancel.getVisibility() == View.VISIBLE || binding.btnOk.getVisibility() == View.VISIBLE) {
                view_divider_h.setVisibility(View.VISIBLE);
//                view_content_bottom_space.setVisibility(View.GONE);
            } else {
                view_divider_h.setVisibility(View.GONE);
//                view_content_bottom_space.setVisibility(View.VISIBLE);
            }
        }

        View view_divider_v = findViewById(R.id.view_divider_v);
        if (view_divider_v != null) {
            if (binding.btnCancel.getVisibility() == View.VISIBLE && binding.btnOk.getVisibility() == View.VISIBLE) {
                view_divider_v.setVisibility(View.VISIBLE);
            } else {
                view_divider_v.setVisibility(View.GONE);
            }
        }
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setMessage(String msg) {
        this.message = msg;
    }

    public void setOKButton(String text, View.OnClickListener listener) {
        okText = text;
        okListener = listener;
    }

    public void setCancelButton(String text, View.OnClickListener listener) {
        cancelText = text;
        cancelListener = listener;
    }

    public void setCustomContentView(View view) {
        this.customContentView = view;
    }

    public void setCustomContentView(View view, ViewGroup.LayoutParams params) {
        this.customContentView = view;
        this.layoutParams = params;
    }
}
